package com.edu.zut.controller;


import com.edu.zut.domain.MenuVo;
import com.edu.zut.entity.Menu;
import com.edu.zut.result.ResponseResult;
import com.edu.zut.service.MenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 菜单表 前端控制器
 * </p>
 *
 * @author 许文科
 * @since 2023-01-12
 */
@RestController
@RequestMapping("/api")
public class MenuController {

    @Autowired
    private MenuService menuService;


    @GetMapping("/getRouters")
    @PreAuthorize("hasRole('teacher')")
    public ResponseResult getRouters() {

//        Long userId = 1L;
        String username = (String) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        List<MenuVo> menuVoList = menuService.selectMenuTreeByUserName(username);
//        System.out.println("user.username()" + username);
        return new ResponseResult("", "", "");
    }


    /**
     * 添加菜单
     *
     * @param menu
     * @return
     */
    @PostMapping("/menu/addMenu")
    public ResponseResult addMenu(@RequestBody Menu menu) {
        return menuService.addMenu(menu);
    }


    /**
     * 菜单
     *
     * @return
     */
    @GetMapping("/menu/queryTreeMenu")
    @PreAuthorize("hasAnyRole('admin','teacher','student')")
    public ResponseResult queryTreeMenu() {
        String username = (String) SecurityContextHolder
                .getContext().getAuthentication().getPrincipal();
        List<MenuVo> menuTree = menuService.selectMenuTreeByUserName(username);
        return new ResponseResult("200", "成功", menuTree);
    }

    /**
     * 路由
     *
     * @return
     */
    @GetMapping("/menu/queryRouter")
    @PreAuthorize("hasAnyRole('admin','teacher','student')")
    public ResponseResult queryRouter() {
        String username = (String) SecurityContextHolder
                .getContext().getAuthentication().getPrincipal();
        return menuService.queryRouter(username);
    }

    /**
     * 获取权限数，权限管理
     */
    @GetMapping("/permission/list")
    @PreAuthorize("hasAnyRole('admin','teacher')")
    public ResponseResult getPermTree() {
        return menuService.getPermTree();
    }

    @GetMapping("/menu/teacher")
    @PreAuthorize("hasRole('admin')")
    public ResponseResult getRoleTeacherPerm() {
        return menuService.getRoleTeacherPerm();
    }

    @GetMapping("/menu/student")
    @PreAuthorize("hasRole('admin')")
    public ResponseResult getRoleStudentPerm() {
        return menuService.getRoleStudentPerm();
    }

}
